package com.emmy.walnut.presenter;

import android.database.sqlite.SQLiteConstraintException;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


import com.emmy.walnut.app.AppDatabase;
import com.emmy.walnut.constant.AppConstant;
import com.emmy.walnut.model.GeneralResult;
import com.emmy.walnut.model.User;
import com.emmy.walnut.presenter.iface.RegPIface;
import com.emmy.walnut.util.MD5Util;
import com.emmy.walnut.view.iface.RegIface;

public class RegPresenter implements RegPIface {

    private RegIface regIface;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case AppConstant.REG_SUCESS:
                    regIface.regSuccess("","");
                    break;
                case AppConstant.REG_FAIL:
                    regIface.regFailed("该用户名已存在，请更换");
                    break;
            }
            return false;
        }
    });

    public RegPresenter(RegIface regIface){
        this.regIface = regIface;
    }

    private RegPresenter(){}

    @Override
    public void register(final String username, final String password) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                User user = AppDatabase.getInstance().userDao().query(username);
                if (user!=null){
                    Message.obtain(handler, AppConstant.REG_FAIL).sendToTarget();
                }else {
                    AppDatabase.getInstance().userDao().insert(new User(username,
                            MD5Util.md5(password)));
                    Message.obtain(handler, AppConstant.REG_SUCESS).sendToTarget();
                }
            }
        }).start();
        /*JSONObject requestData = new JSONObject();
        try {
            requestData.put("nickname", username);
            requestData.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestData.toString());
        RetrofitHelper.getInstance().serviceApi().reg(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<GeneralResult>() {
                    @Override
                    public void onCompleted() {
                        Log.e("retrofit","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("retrofit",e.toString());
                        Log.e("retrofit","onError");
                    }

                    @Override
                    public void onNext(GeneralResult loginAndRegResult) {
                        if (loginAndRegResult.getSuccess())
                            regIface.regSuccess(loginAndRegResult.getUUid(), loginAndRegResult.getAccessKey());
                        else
                            regIface.regFailed(loginAndRegResult.getError_msg());
                    }
                });*/
    }
}
